package controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import dao.LoaisanphamDAO;
import dao.NhomsanphamDAO;
import dao.SanphamDAO;
import dao.TaikhoanDAO;

import pojo.Loaisanpham;
import pojo.Taikhoan;
import util.SearchVisitor;

@ManagedBean(name = "quanLyTaiKhoanBean")
@ViewScoped
public class QuanLyTaiKhoanBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private List<Taikhoan> lstTaiKhoan;
	private List<String> lstKiTuBatDau;
	private String kiTuBatDau;
	private String chuoiTimKiem;

	@PostConstruct
	public void init() {
		setLstTaiKhoan(new ArrayList<Taikhoan>());
		GenerateKiTuBatDauList();
	}

	public void LietKeToanBo() {
		TaikhoanDAO tkdao = new TaikhoanDAO();
		lstTaiKhoan = tkdao.getList();
		System.out.println("Tai Khoan Xong " + lstTaiKhoan.size());
	}

	public void LietKeTheoKiTuBatDau() {
		System.out.println("Liet Ke theo Ki tu: " + getKiTuBatDau());
		TaikhoanDAO tkdao = new TaikhoanDAO();
		lstTaiKhoan = tkdao.GetTaiKhoanTheoKiTuBatDau(getKiTuBatDau());
	}

	private void GenerateKiTuBatDauList() {
		lstKiTuBatDau = Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H",
				"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
				"U", "V", "W", "X", "Y", "Z");
	}

	public void TimKiem() {
		System.out.println("Tim Kiếm TK: " + getChuoiTimKiem());
		TaikhoanDAO tkdao = new TaikhoanDAO();
		SearchVisitor sv = new SearchVisitor();
		tkdao.accept(sv, getChuoiTimKiem());
		lstTaiKhoan = sv.getResultTaiKhoan();
		System.out.println("Tim Kiếm KQ: " + lstTaiKhoan.size());
	}

	public void BanNick(Taikhoan tk)
	{
		System.out.println("Ban Nick: " + tk.getHoten());
		TaikhoanDAO tkdao = new TaikhoanDAO();
		//tkdao.delete(tk);
		tk.setBanned(true);
		tkdao.saveOrUpdate(tk);
		ClearLstTaiKhoan();
	}
	public void HuyBanNick(Taikhoan tk)
	{
		System.out.println("Ban Nick: " + tk.getHoten());
		TaikhoanDAO tkdao = new TaikhoanDAO();
		//tkdao.delete(tk);
		tk.setBanned(false);
		tkdao.saveOrUpdate(tk);
		ClearLstTaiKhoan();
	}	
	
	private void ClearLstTaiKhoan()
	{
		getLstTaiKhoan().clear();
	}
	/**
	 * @return the lstTaiKhoan
	 */
	public List<Taikhoan> getLstTaiKhoan() {
		return lstTaiKhoan;
	}

	/**
	 * @param lstTaiKhoan
	 *            the lstTaiKhoan to set
	 */
	public void setLstTaiKhoan(List<Taikhoan> lstTaiKhoan) {
		this.lstTaiKhoan = lstTaiKhoan;
	}

	/**
	 * @return the lstKiTuBatDau
	 */
	public List<String> getLstKiTuBatDau() {
		return lstKiTuBatDau;
	}

	/**
	 * @param lstKiTuBatDau
	 *            the lstKiTuBatDau to set
	 */
	public void setLstKiTuBatDau(List<String> lstKiTuBatDau) {
		this.lstKiTuBatDau = lstKiTuBatDau;
	}

	public String getKiTuBatDau() {
		return kiTuBatDau;
	}

	public void setKiTuBatDau(String kiTuBatDau) {
		this.kiTuBatDau = kiTuBatDau;
	}

	public String getChuoiTimKiem() {
		return chuoiTimKiem;
	}

	public void setChuoiTimKiem(String chuoiTimKiem) {
		this.chuoiTimKiem = chuoiTimKiem;
	}

}
